<?php /* УТФ-8:fix */

class SPIcontroller__catalogs__front extends SPIcontroller__catalogs
{
	
	public function exec( & $content ){
		if( $content['env_vars']['tree'] )
			$this->exec_tree( $content );
		else{
			$content['data']['items'] = $content['menu']['directions']['items'];   // для менюшки будут уже выбраны все корневые каталоги, потому просто присваиваем тут.
			$content['topic_template_filename'] = 'list.tpl';
		}
		return true;
	}
	
	public function get_main_catalogs (){
		$data = $this->generate_catalog_items( null, true );
		return $data;
	}
	
	private function exec_tree ( & $content ){
		$cur_id = $content['env_vars']['tree'];
		$current = SPIm::I('catalogs')->front->get_by_id($cur_id);
		$content['data']['current'] = $current;
		
		$this->switch_type($data_parrent, $current['type'], $cur_id, false, $cur_id);   // заполнит $items['content'] данными из текущего каталога
		
		if(count($data_parrent))
			$items['content'] = $data_parrent;
		
		$content['data']['subitems'] = $items;
		
		$content['topic_template_filename'] = array (
			'articles'	=>	'list.tpl',
			'faq'		=>	'faq.tpl',	
			'gallery' 	=>	'gallery.tpl',	
			'news' 		=>	'list.tpl',	
			'text' 		=>	'text.tpl',	
			'video' 	=>	'list.tpl',		
			'expert' 	=>	'list.tpl',			
			'club' 		=>	'list.tpl',			
			'tests' 	=>	'list.tpl',			
			'votes' 	=>	'votes.tpl',			
			'seminars' 	=>	'list.tpl',	
			'tour' 		=>	'list.tpl',			
			'form'		=>	'form.tpl',	
		);
		return true;
	}
	
	private function switch_type(& $items, $type, $id, $array = false, $cur_tree = false){
		switch($type)
		{
			case 'articles' :
			case 'faq' :
			case 'gallery' :
			case 'news' :
			case 'text' :
			case 'video' :
				$item = SPIc::I('content')->front->get_by_treeid($id, $type, $cur_tree);
			break;
			case 'expert' :
				$item = SPIc::I('peoples')->front->get_by_treeid($id, $cur_tree);
			break;
			case 'club' :
				$item = SPIc::I('club')->front->get_by_treeid($id, $cur_tree);
			break;
			case 'tests' :
				$item = SPIc::I('tests')->front->get_by_treeid($id, $cur_tree);
			break;
			case 'votes' :
				$item = SPIc::I('votes')->front->get_by_treeid($id);
			break;
			case 'seminars' :
			case 'tour' :
				$item = SPIc::I('seminars')->front->get_by_treeid($id, $type, $cur_tree);
			break;
			case 'form' :
				$item = SPIc::I('forms')->front->get_by_treeid($id, $cur_tree);
			break;
			case 'link' :
				$current = SPIm::I('catalogs')->front->get_by_id($id, $cur_tree);
				$item = array($current);
			break;
			default :
				$item = 'Тип неопознан!!!';
			break;
		}
		
		if($array)
			$items[$type] = $item;
		else
			$items = $item;
	}
	
	public function generate_catalog_items($tree_id, $without_empty = null)
	{	
		$tree_ids = SPIc::I('catalogs')->front->get_catalog_items( $tree_id );

		foreach( $tree_ids as $id ){
			$item = SPIm::I('catalogs')->front->get_by_id( $id );
			if( $item['type'] !== 'link' )
				$item['link'] = SPIc::I('front')->link->catalog( $id );
				
			$item['preview'] = SPIm::I('media')->get_public_source_by_index(
				SPIc::I('catalogs')->MEDIA_GID, $item['id'], 1,
				array( 'mode' => 'preview' )
			);
				
			$tree_items[] = $item;
		}
		
		$count_tree_items = count($tree_items);
		
		if( is_null( $without_empty ) ){
			if ( count( $tree_items ) < 6 || $count_tree_items % 6  ){
				
				$count_group = ( (int)($count_tree_items/6) ) + 1;
				
				for( $i = count( $tree_items ); $i<( $count_group*6 ); $i++ ){
					$empty_item['empty'] = true;
					$tree_items[] = $empty_item;
				}
			}
		}
		return array ( 'items' => $tree_items, 'count_tree_items' => $count_tree_items, 'count_items' => count($tree_items) ) ;
	}
	
	public function build_tree( & $tree, $pid = null, $tree_id=0 )
	{	
		$qresult = SPIm::I('catalogs')->front->select( null, $pid );
		
		if( count($qresult)>0 ){
			foreach( $qresult as $id )
			{	
				$i = SPIm::I('catalogs')->front->get_by_id($id);
				$this-> build_tree($i['items'], $id, $tree_id);
				if(empty($i['link']))
					$i['link'] = SPIc::I('front')->link->catalog($id, $i['name']);
				if($i['id'] == $tree_id)
					$i['is_active'] = true;
				//if($pid == null &&($this->get_index_parrent_tree_by_tree_id( $tree_id ) == $i['id']) ){
				//	$i['is_open'] = 5;
				//}
				$tree[] = $i;
			}
		}
		else{
			return $tree;
		}
	}
	
	public function build_child_catalog_list( & $tree = null, $pid = null)
	{	
		$tree[] = $pid;
		$qresult = SPIm::I('catalogs')->front->select( null, $pid );
		
		if( count($qresult)>0 ){
			foreach( $qresult as $id )
			{	
				$this-> build_child_catalog_list($tree, $id);				
			}
		}
		else{
			return $tree;
		}
	}
	
	public function get_catalog_items( $id )
	{	
		$qresult = SPIm::I('catalogs')->front->select( null, $id );
		$list_item = array();
		foreach( $qresult as $item_id )
		{	
			$list_item[] = $item_id;	
		}		
		return $list_item;
	}
	
	public function get_index_parrent_tree_by_tree_id( $id )
	{	
		$result = SPIm::I('catalogs')->front->get_by_id($id );
		
		while( $result['pid'] !== null )
		{				
			$result = SPIm::I('catalogs')->front->get_by_id( $result['pid'] );
		}
		return $result['id'];
	}
	
	public function get_fuul_way_catalog( $id ){
		$way = array();
		
		if( !is_null( $id ) ){
			$result = SPIm::I('catalogs')->front->get_by_id( $id );
			$item['link'] = SPIc::I('front')->link->catalog( $result['id'], $result['name'] );
			$item['name'] = $result['name'];
			$item['anons'] = $result['anons'];
			$way[] = $item;
		}
		
		while( $result['pid'] !== null )
		{				
			$result = SPIm::I('catalogs')->front->get_by_id( $result['pid'] );
			$item['link'] = SPIc::I('front')->link->catalog( $result['id'], $result['name'] );
			$item['name'] = $result['name'];
			$item['anons'] = $result['anons'];
			$way[] = $item;
		}
		$way = array_reverse($way);
		return $way;
	}
	
	/*
	public function build_list( $select_mode, $pid_catalog = null, $active_catalog_id = null )
	{
		$result = array();
		
		$orderby['field'] = 'order_index';
		$orderby['dir'] = '+';
		
		$qresult = SPIm::I('catalogs')->front->select( $select_mode, $pid_catalog, null, $orderby );
		if(count($qresult) > 0)
		foreach ($qresult as $i){
			$i['link'] = SPIc::I('front')->link->catalog_content( 'faq', $i['id'], $i['name'] );
			$i['is_active'] = $active_catalog_id !== null && $i['id'] == $active_catalog_id;

			$result[] = $i;
		}
		if ($active_catalog_id === false){
			$result[0]['is_active']=true;
		}

		return $result;
	}
	
	
	
	public function get_by_id($id){
		$item = SPIm::I('catalogs')->front->get_by_id($id);
		return $item;
	}*/
	
	
}
